package mistic.storage;

import javax.swing.JFrame;
import javax.swing.JPanel;

//import java.awt.BorderLayout;
import java.awt.Color;
//import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.ButtonGroup;
import javax.swing.JPasswordField;

import java.security.PrivilegedAction;
import javax.security.auth.Subject;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.TextOutputCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;
import java.awt.Insets;
import validate.PasswordHasher;
import validate.PasswordHasher.PasswordHash;
import mistic.storage.Registro;
import mistic.storage.Storage;

public class JCreacionUsr extends JFrame implements CallbackHandler {
	// Paneles generales
	private JPanel jPanel = null;
	private JPanel jpBotonesAcepSalir = null;
	//private JPanel jpBotones = null;
	// Botones de Aceptar, cancelar y salir
	private JButton jbAceptar = null;
	private JButton jbCancelar = null;
	private JButton jbSalir = null;
	// Areas de texto para realizar la busqueda
	private JTextField jtfUsuario = null;
	private JTextField jpwPassword = null;
	// labes varios
	private JLabel jlPassword = null;
	private JLabel jlNombreUsr = null;
	private ButtonGroup buttonGroup = null; // grupo de Radio Button para manejar opciones  //  @jve:decl-index=0:
	
	public String rol1="0", rol2="0", rol3="0", rol4="0", rol5="0", rol6="0";  //  @jve:decl-index=0:
	public String usr="";
	public String pwd="";  //  @jve:decl-index=0:
	
	private JPanel jPanel1 = null;
	private JCheckBox jCheckBoxRol2 = null;
	private JCheckBox jCheckBoxRol3 = null;
	private JPanel jPanelRoles = null;
	private JCheckBox jCheckBoxRol4 = null;
	private JCheckBox jCheckBoxRol5 = null;
	private JCheckBox jCheckBoxRol6 = null;
	private JCheckBox jCheckBoxRol1 = null;
	// Constructor 
	public JCreacionUsr() throws Exception {
		super();
		initialize();
		addWindowListener(new EscuchaCerrar());		
	}
	// Metodo que inicializa y setea contexto
	private void initialize() throws Exception {
		this.setContentPane(getJPanel());
		this.setResizable(true);
		this.setComponentOrientation(java.awt.ComponentOrientation.RIGHT_TO_LEFT);
		//this.setMinimumSize(new java.awt.Dimension(217,82));
		this.setTitle("Creacion de Usuarios");
		this.setSize(537, 232);
		this.setVisible(true);
		this.getButtonGroup();
		jpwPassword.setEditable(true); 
		jtfUsuario.setEditable(true);  
		//buscar(mySql,"Interno");
	}
	// panel principal donde se carga todo el look and feel de la aplicacion
	private JPanel getJPanel()throws Exception  {
		if (jPanel == null) {
			GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
			gridBagConstraints14.gridx = 0;
			gridBagConstraints14.gridy = 11;
			GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
			gridBagConstraints12.gridx = 11;
			gridBagConstraints12.gridy = 11;
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			gridBagConstraints10.gridx = 10;
			gridBagConstraints10.gridy = 11;
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.gridx = 9;
			gridBagConstraints8.gridy = 11;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 6;
			gridBagConstraints.gridy = 11;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridx = 4;
			gridBagConstraints5.gridy = 6;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.anchor = GridBagConstraints.WEST;
			gridBagConstraints3.gridx = 5;
			gridBagConstraints3.gridy = 11;
			gridBagConstraints3.insets = new Insets(0, 20, 10, 0);
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 8;
			gridBagConstraints2.gridy = 11;
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();

			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
			jPanel.setBackground(Color.lightGray);
			
			gridBagConstraints1.gridx = 1;
			gridBagConstraints1.gridy = 16;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.weighty = 1.0;
			gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
			gridBagConstraints1.gridwidth = 3;
			gridBagConstraints1.insets = new java.awt.Insets(0,8,0,8);
			gridBagConstraints4.gridx = 1;
			gridBagConstraints4.gridy = 31;
			gridBagConstraints4.gridheight = 2;
			gridBagConstraints4.anchor = java.awt.GridBagConstraints.CENTER;
			gridBagConstraints4.gridwidth = 9;
			gridBagConstraints4.fill = java.awt.GridBagConstraints.NONE;
			gridBagConstraints4.insets = new java.awt.Insets(4,0,4,0);
			gridBagConstraints6.gridx = 1;
			gridBagConstraints6.gridy = 4;
			gridBagConstraints6.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints6.gridwidth = 3;
			gridBagConstraints6.insets = new java.awt.Insets(8,8,0,8);
			gridBagConstraints11.gridx = 0;
			gridBagConstraints11.gridy = 5;
			gridBagConstraints11.insets = new java.awt.Insets(0,8,0,8);
			gridBagConstraints11.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints11.gridwidth = 12;
			jPanel.add(getjpBotonesAcepSalir(), gridBagConstraints4);
			jPanel.add(getJPanel1(), gridBagConstraints11);
			jPanel.add(getJCheckBoxRol3(), gridBagConstraints2);
			
			jPanel.add(getJPanelRoles(), gridBagConstraints5);
			jPanel.add(getJCheckBoxRol2(), gridBagConstraints);
			jPanel.add(getJCheckBoxRol4(), gridBagConstraints8);
			jPanel.add(getJCheckBoxRol5(), gridBagConstraints10);
			jPanel.add(getJCheckBoxRol6(), gridBagConstraints12);
			jPanel.add(getJCheckBoxRol1(), gridBagConstraints14);
			jPanel.addPropertyChangeListener(new java.beans.PropertyChangeListener() { 
				public void propertyChange(java.beans.PropertyChangeEvent e) { 
					if ((e.getPropertyName().equals("enabled"))) { 
						System.out.println("propertyChange(enabled)"); 
					} 
				}
			});
		}
		return jPanel;
	}
// Este panel realiza un marco en color azul oscuro con las areas de texto a modo estetico
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints19 = new GridBagConstraints();
			
			jlNombreUsr = new JLabel();
			jlPassword = new JLabel();
			jlNombreUsr.setForeground(Color.white);
			jlPassword.setForeground(Color.white);
		
			gridBagConstraints7.gridx = 1;
			gridBagConstraints7.gridy = 8;
			gridBagConstraints7.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints7.fill = java.awt.GridBagConstraints.NONE;
			gridBagConstraints7.insets = new java.awt.Insets(0,10,3,15);
			
			gridBagConstraints9.gridx = 2;
			gridBagConstraints9.gridy = 9;
			gridBagConstraints9.weightx = 1.0;
			gridBagConstraints9.fill = java.awt.GridBagConstraints.NONE;
			gridBagConstraints9.gridwidth = 6;
			gridBagConstraints9.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints9.insets = new java.awt.Insets(0,0,1,0);

			gridBagConstraints13.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints13.insets = new java.awt.Insets(0,15,1,0);
			gridBagConstraints13.gridx = 1;
			gridBagConstraints13.gridy = 9;
			
			
			gridBagConstraints19.gridx = 2;
			gridBagConstraints19.gridy = 8;
			gridBagConstraints19.weightx = 1.0;
			gridBagConstraints19.fill = java.awt.GridBagConstraints.NONE;
			gridBagConstraints19.anchor = java.awt.GridBagConstraints.WEST;
			
			jlNombreUsr.setText("Nombre de Usuario");
			jlNombreUsr.setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD, 14));
			jlPassword.setText("Ingrese Contrasena      ");
			jlPassword.setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD, 14));
			
			
			jPanel1 = new JPanel();
			jPanel1.setLayout(new GridBagLayout());
			jPanel1.setBackground(Color.lightGray.darker());
			jPanel1.add(jlNombreUsr, gridBagConstraints7);
			jPanel1.add(jlPassword, gridBagConstraints13);
			jPanel1.add(getJPasswordField(), gridBagConstraints9);
			jPanel1.add(getJTextField(), gridBagConstraints19);
		}
		return jPanel1;
	}	
	
	
	// panel que realiza el marco color azul oscuro de los botones de Aceptar y salir
	private JPanel getjpBotonesAcepSalir()throws Exception {
		if (jpBotonesAcepSalir == null) {
			jpBotonesAcepSalir = new JPanel();
			jpBotonesAcepSalir.setBackground(Color.blue.brighter());
			jpBotonesAcepSalir.setMinimumSize(new Dimension(250, 39));
			jpBotonesAcepSalir.add(getJButton(), null);
			jpBotonesAcepSalir.add(getJButton2(), null);
		}
		return jpBotonesAcepSalir;
	}

	private JButton getJButton() throws Exception 
	{

		if (jbAceptar == null) 
		{
			jbAceptar = new JButton();
			jbAceptar.setText("Aceptar");
			jbAceptar.addActionListener(new java.awt.event.ActionListener() 
					{ 
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					try {
						 System.out.println("Se presiono Aceptar");
						 // Valido el Boton presionado para setear los parametros a enviar
							usr= (jtfUsuario.getText());
							pwd= (jpwPassword.getText());
							
							System.out.printf(usr);
						// buscar(mySql,Type);// Le envio al metodo Buscar los parametros seleccionados.
					} 
					catch(Exception ex) { 
						ex.printStackTrace();
					}
				}
					});
		}
		return jbAceptar;
	 }

// este boton cierra la ventana
	private JButton getJButton2() {
		if (jbSalir == null) {
			jbSalir = new JButton();
			jbSalir.setText("Salir");
			jbSalir.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) { 
					System.out.println("Se presiono Salir");
					setVisible(false);
					dispose();
					System.exit(0);
				}
			});
		}
		return jbSalir;
	}

	private JTextField getJTextField() {
		if (jtfUsuario == null) {
			jtfUsuario = new JTextField();
			jtfUsuario.setPreferredSize(new java.awt.Dimension(136,20));
			jtfUsuario.setMinimumSize(new Dimension(130, 20));
			jtfUsuario.addActionListener(new java.awt.event.ActionListener() { 
				// presionando enter en el area de texto le envia los parametros al metodo aceptar al 
				// igual que el boton aceptar
				public void actionPerformed(java.awt.event.ActionEvent e) { 
					try {
						 System.out.println("Se presiono Aceptar - El usuario es: ");
						 System.out.printf(jtfUsuario.getText());
							
						  //buscar(jtfUsuario.getText(),Type);
					} 
					catch(Exception ex) { 
						ex.printStackTrace();
					}
				}
					});
		}
		return jtfUsuario;
	}

	private JPasswordField getJPasswordField() {

				///////////////////////////////////
				JPasswordField jpwPassword = new JPasswordField(12);
				jpwPassword.setEchoChar('*');
				jpwPassword.addActionListener(new ActionListener() {

					public void actionPerformed(ActionEvent e) {
						JPasswordField input = (JPasswordField) e.getSource();
						char[] password = input.getPassword();
		
						 System.out.println("Se presiono Aceptar - El password es:");
						 System.out.println(input.getPassword());
			//		pwd=	jpwPassword.getText();
						/*	if (isPasswordCorrect(password)) {
							JOptionPane.showMessageDialog(frame,
									"Correct  password.");
						} else {
							JOptionPane.showMessageDialog(frame,
									"Sorry. Try again.", "Error Message",
									JOptionPane.ERROR_MESSAGE);
						}*/
				//	}
				//});
				///////////////////////////////////
				
	//			public void actionPerformed(java.awt.event.ActionEvent e) { 
					try {
						 System.out.println("Se presiono Aceptar");
					//	 buscar(jpwPassword.getText(),Type);
					} 
					catch(Exception ex) { 
						ex.printStackTrace();
					}
				}
					});
				return jpwPassword;

		}

	// este grupo permite que la seleccion de los radio buttons sea una a la vez
	private ButtonGroup getButtonGroup() {
		if (buttonGroup == null) {
			buttonGroup = new ButtonGroup();
			jpwPassword.setEditable(true);
			jtfUsuario.setEditable(true);
		}
		return buttonGroup;
	}
	// este metodo permite cerrar la aplicacion
	class EscuchaCerrar extends WindowAdapter{
		public void windowClosing(WindowEvent e){
			System.out.print("salir");
			setVisible(false);
			dispose();
			System.exit(0);
		}// fin windowClosing
	}// fin EscuchaCerrar


public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for(Callback cb : callbacks) {
        if (cb instanceof NameCallback) {
            //The user can provide this information using the console or a GUI.
            ((NameCallback) cb).setName(jtfUsuario.getText()); //The user can provide this information using the console or a GUI.
        } else if (cb instanceof PasswordCallback) {
            //The user can provide this information using the console or a GUI.
            ((PasswordCallback) cb).setPassword("password".toCharArray());
        } else if (cb instanceof TextOutputCallback) {
            System.out.println(((TextOutputCallback) cb).getMessage());
        }
    }
    }
		  /**
 * This method initializes jCheckBoxRol2	
 * 	
 * @return javax.swing.JCheckBox	
 */
private JCheckBox getJCheckBoxRol2() {
	if (jCheckBoxRol2 == null) {
		jCheckBoxRol2 = new JCheckBox();
		jCheckBoxRol2.setText("Rol 2");
		jCheckBoxRol2.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				System.out.println("actionPerformed(Rol2)"); // TODO Auto-generated Event stub actionPerformed()
				String rol2="1";
				System.out.println(rol2); 
				
			}
		});
	}
	return jCheckBoxRol2;
}
		/**
		 * This method initializes jCheckBoxRol3	
		 * 	
		 * @return javax.swing.JCheckBox	
		 */
		private JCheckBox getJCheckBoxRol3() {
			if (jCheckBoxRol3 == null) {
				jCheckBoxRol3 = new JCheckBox();
				jCheckBoxRol3.setText("Rol 3");
				jCheckBoxRol3.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						System.out.println("actionPerformed(rol3)"); // TODO Auto-generated Event stub actionPerformed()
						String rol3="1";
						System.out.println(rol3); 
					}
				});
			}
			return jCheckBoxRol3;
		}
		/**
		 * This method initializes jPanelRoles	
		 * 	
		 * @return javax.swing.JPanel	
		 */
		private JPanel getJPanelRoles() {
			if (jPanelRoles == null) {
				jPanelRoles = new JPanel();
				jPanelRoles.setLayout(new GridBagLayout());
			}
			return jPanelRoles;
		}
		/**
		 * This method initializes jCheckBoxRol4	
		 * 	
		 * @return javax.swing.JCheckBox	
		 */
		private JCheckBox getJCheckBoxRol4() {
			if (jCheckBoxRol4 == null) {
				jCheckBoxRol4 = new JCheckBox();
				jCheckBoxRol4.setText("Rol 4");
				jCheckBoxRol4.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						System.out.println("actionPerformed(rol4)"); // TODO Auto-generated Event stub actionPerformed()
						String rol4="1";
						System.out.println(rol4); 
					}
				});
			}
			return jCheckBoxRol4;
		}
		/**
		 * This method initializes jCheckBoxRol5	
		 * 	
		 * @return javax.swing.JCheckBox	
		 */
		private JCheckBox getJCheckBoxRol5() {
			if (jCheckBoxRol5 == null) {
				jCheckBoxRol5 = new JCheckBox();
				jCheckBoxRol5.setText("Rol 5");
				jCheckBoxRol5.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						System.out.println("actionPerformed(rol5)"); // TODO Auto-generated Event stub actionPerformed()
						String rol5="1";
						System.out.println(rol5); 
						
					}
				});
			}
			return jCheckBoxRol5;
		}
		/**
		 * This method initializes jCheckBoxRol6	
		 * 	
		 * @return javax.swing.JCheckBox	
		 */
		private JCheckBox getJCheckBoxRol6() {
			if (jCheckBoxRol6 == null) {
				jCheckBoxRol6 = new JCheckBox();
				jCheckBoxRol6.setText("Rol 6");
				jCheckBoxRol6.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						
					String rol6="1";
					System.out.println(rol6); 
					System.out.println("actionPerformed(rol6)");
					}
				});
			}
			return jCheckBoxRol6;
		}
		/**
		 * This method initializes jCheckBoxRol1	
		 * 	
		 * @return javax.swing.JCheckBox	
		 */
		private JCheckBox getJCheckBoxRol1() {
			if (jCheckBoxRol1 == null) {
				jCheckBoxRol1 = new JCheckBox();
				jCheckBoxRol1.setText("Rol 1");
				jCheckBoxRol1.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						System.out.println("actionPerformed(rol1)"); // TODO Auto-generated Event stub actionPerformed()
						String rol1="1";
						System.out.println(rol1); 
					}
				});
			}
			return jCheckBoxRol1;
		}
		public static void main(String[] args) throws Exception 
	{
 	  JCreacionUsr bt= new JCreacionUsr();
    	bt.show();
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"   

